/**
 * navProvider module
 */


 var navProvider = {
     nav: {
         available: true,
         page: {}
     },
     setNav: function (mode, pageKey){
         var page;
         this.nav.mode = mode;
         this.redirectWhenUnavailable(mode, pageKey);
         if (pageKey){
             page = pagesProvider.getPageByKey(pageKey, mode);
             pagesProvider.updateDisabledStatus(page, this.nav.mode);
             this.nav.page = page;
             this.nav.pageKey = pageKey;
         } else {
             this.nav.page = null;
             this.nav.pageKey = null;
         }
         this.setRightPane(this.getRightPaneUrl(this.nav.page));
         return this.nav;
     },
     getRightPaneUrl: function (page){
         if (page && page.disabled) {
             return templates.panes[nav.mode + '_disabled'];
         }
         if (page && nav.mode === 'stats') {
             return templates.panes.stats_page;
         }
         if (page && nav.mode === 'edit' && !!page.include_source) {
             return templates.panes.included_page;
         }
         if (page && page.rejected) {
             return templates.panes.rejected_page;
         }
         if (page && campaignProvider.campaign.is_archived) {
             return templates.panes.archived_page;
         }
         if (!page && nav.mode === 'edit') {
             return templates.panes.add_pages_info;
         }
         if (!nav.available || -1 !== ['settings', 'collaborate'].indexOf(nav.mode)) {
             return null;
         }
         return templates.panes[nav.mode];
     },
     path: function (){
         var args = [].slice.call(arguments, 0);
         return args.join('/') + '/';
     },
     setRightPane: function (templateUrl){
         if (templateUrl !== nav.rightPaneUrl) {
             nav.rightPaneUrl = templateUrl;
         }
     },
     updateTitle: function (){
         this.setTitle(nav._titleCache);
     },
     setTitle: function (title){
         nav._titleCache = title;
         var campaigntitle = campaignProvider.campaign.vision_campaign_title;
         title = title.replace('{campaign}', campaigntitle);
         if (nav.page) {
             title = title.replace('{page}', nav.page.title);
         }
         document.title = title;
     },
     setNextPageKey: function (key){
         nav.nextPageKey = key;
     },
     goElsewhere: function (key){
         nav.newPageType = 'new';
         if (nav.page && nav.page.page_id === key){
             $location.path(paths.pathFor.template + '/');
             $location.replace();
         }
     }
 };

 module.exports = navProvider;
